﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LDSP.BRL
{
    public class BaseValidUserPage : BasePage
    {
        public bool IsSysAdmin { get { return LoginAdmin != null; } }

        public override void RedirectBefore()
        {
            //如果不是系统里合法的用户，则退出到登陆页面。
            if (LoginUser == null && LoginAdmin == null)
            {
                this.GoToLogin();
            }
            else if (LoginUser != null)
            {
                //用户被删除后，即使已登录了也退出
                LoginUser.GetProperties(LoginUser.ID);
                if (LoginUser.ID == 0)
                {
                    this.GoToLogin();
                }
            }
        }

        protected override int GetPageSizeSetting()
        {
            int pageSize = 10;
            int userId = IsSysAdmin == true ? LoginAdmin.ID : LoginUser.ID;
            string page = Request.Url.ToString();

            PageSizeSetting setting = new PageSizeSetting().GetPageSizeByUserAndPage(userId, page);
            if (setting != null)
            {
                pageSize = setting.Count;
            }

            return pageSize;
        }

        protected override void SavePageSizeSetting()
        {
            int userId = IsSysAdmin == true ? LoginAdmin.ID : LoginUser.ID;
            string page = Request.Url.ToString();
            int count = g_pageInfo.PageSize;

            PageSizeSetting setting = new PageSizeSetting().GetPageSizeByUserAndPage(userId, page);
            if (setting == null)
            {
                setting = new PageSizeSetting();
                setting.UserId = userId;
                setting.Page = page;
                setting.Count = count;
                setting.Add();
            }
            else
            {
                setting.Count = count;
                setting.Update();
            }
        }
    }
}